<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;

use App\Logics\ChannelLogic;
use App\Models\ChannelModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Logics\CategoryLogic;
use App\Logics\GoodsLogic;
use App\Logics\CouponLogic;


use Illuminate\Support\Facades\Storage;
class CouponController extends BaseController{



    private function getInstance(){
        static $ob=null;
        if(is_null($ob)){
            $ob= new CouponLogic();
        }
        return $ob;
    }
    //优惠券列表
    public function index()
    {

        $page = (int)request("page", 1);
        $per_page = (int)request("per_page", 20);
        $cou_name = trim(request('cou_name'));
        $cou_id = (int)request('cou_id',0);
        $cou_status = (int)request('cou_status',-1);
        $is_breaks = (int)request('is_breaks',-1);

        $para=[
            'page'=>$page,
            'per_page'=>$per_page,
            'cou_name'=>$cou_name,
            'cou_id'=>$cou_id,
            'cou_status'=>$cou_status,
            'is_breaks'=>$is_breaks
        ];
        $ob= $this->getInstance();
        $data=$ob->index($para);
        if($data && is_string($data)) die(json_error($data));
        $content= view("admin.coupon.index",$data) ;
        $data=[
            'content'=>$content,
            'header'=>'优惠券列表',
        ];
        return  $this->baseList($data);
    }

    //优惠券添加
    public function add(Request $request){
        if($_POST){
            $cou_name=trim(request('cou_name'));
            $cou_effect=trim(request('cou_effect'));
            $cou_describe=trim(request('cou_describe'));
            $cha_name=trim(request('cha_name'));
            $cou_start_time=trim(request('cou_start_time'));
            $cou_end_time=trim(request('cou_end_time'));
            $cou_sum_price=trim(request('cou_sum_price'));
            $cou_price=trim(request('cou_price'));
            $goods_id=trim(request('goods_id'));

            $cou_status=(int)request('cou_status');
            $is_breaks=(int)request('is_breaks');
            $cou_every_count=(int)request('cou_every_count');
            $cou_expiration=(int)request('cou_expiration');
            $cou_type=(int)request('cou_type');

            $para=[
                'cou_name'=>$cou_name,
                'cou_effect'=>$cou_effect,
                'cou_describe'=>$cou_describe,
                'cha_name'=>$cha_name,
                'cou_start_time'=>$cou_start_time,
                'cou_end_time'=>$cou_end_time,
                'cou_sum_price'=>$cou_sum_price,
                'cou_price'=>$cou_price,
                'goods_id'=>$goods_id,
                'cou_status'=>$cou_status,
                'is_breaks'=>$is_breaks,
                'cou_every_count'=>$cou_every_count,
                'cou_expiration'=>$cou_expiration,
                'cou_type'=>$cou_type
            ];
            $ob= $this->getInstance();
            $data= $ob->add($para);
            $this->result($data);
        }



        $channel= ChannelModel::pluck('cha_describe','cha_name');
        $channel=ob_to_array($channel);
        $content= view("admin.coupon.add",['channel'=>$channel]) ;
        $data=[
            'content'=>$content,
            'header'=>'优惠券添加',
        ];
        return  $this->baseList($data);
    }

    public function getCategory(){
        $name=trim(request('cha_name'));
        $ob=$this->getInstance();
        $data= $ob->getCategory();
        $this->result($data);
    }

    //编辑 优惠券
    public function edit(Request $request){
        if($_POST){
            $cou_name=trim(request('cou_name'));
            $cou_effect=trim(request('cou_effect'));
            $cou_describe=trim(request('cou_describe'));
            $cha_name=trim(request('cha_name'));
            $cou_start_time=trim(request('cou_start_time'));
            $cou_end_time=trim(request('cou_end_time'));
            $cou_sum_price=trim(request('cou_sum_price'));
            $cou_price=trim(request('cou_price'));
            $goods_id=trim(request('goods_id'));
            $cou_status=(int)request('cou_status');
            $is_breaks=(int)request('is_breaks');
            $cou_every_count=(int)request('cou_every_count');
            $cou_expiration=(int)request('cou_expiration');
            $cou_type=(int)request('cou_type');
            $cou_id=(int)request('cou_id');

            $para=[
                'cou_name'=>$cou_name,
                'cou_effect'=>$cou_effect,
                'cou_describe'=>$cou_describe,
                'cha_name'=>$cha_name,
                'cou_start_time'=>$cou_start_time,
                'cou_end_time'=>$cou_end_time,
                'cou_sum_price'=>$cou_sum_price,
                'cou_price'=>$cou_price,
                'goods_id'=>$goods_id,
                'cou_status'=>$cou_status,
                'is_breaks'=>$is_breaks,
                'cou_every_count'=>$cou_every_count,
                'cou_expiration'=>$cou_expiration,
                'cou_type'=>$cou_type,
                'cou_id'=>$cou_id
            ];
            $ob= $this->getInstance();
            $data= $ob->edit($para);
            $this->result($data);
        }


        $cou_id=(int)request('cou_id');
        $ob=$this->getInstance();
        $data=$ob->getEdit($cou_id);
        if($data && is_string($data)) die(json_error($data));
        $content= view("admin.coupon.edit", $data) ;
        $data=[
            'content'=>$content,
            'header'=>'优惠券编辑',
        ];
        return  $this->baseList($data);
    }


    //删除优惠券
    public function delete(){
        if($_POST){
          $cou_id=(int)request('cou_id');
          $data=CouponLogic::delete($cou_id);
          $this->result($data);
        }
    }





}